from django.db import models

# Create your models here.


class User(models.Model):
    name = models.CharField(max_length=80)
    age = models.IntegerField()

    def __str__(self):
        return self.name


class Follow(models.Model):
    # _set 数据
    # 关联表名
    idol = models.ForeignKey(User,on_delete=models.CASCADE,related_name='idol')
    fans = models.ForeignKey(User,on_delete=models.CASCADE,related_name='fans')

    class Meta:
        unique_together = ('idol','fans')   # 联合约束  同一条数据只允许记录一次

    def __str__(self):
        return "%s关注了%s" % (self.fans.name,self.idol.name)
'''
    >>> u1 = User.objects.create(name='张三',age=18)
    >>> u2 = User.objects.create(name='李四',age=18)
    >>> u3 = User.objects.create(name='王五',age=18)
    >>> u4 = User.objects.create(name='赵六',age=18)
    >>> u5 = User.objects.create(name='徐七',age=18)
'''
# u3 => u1 u2
# u1 => u3 u4 u5
# u5 => u3

#Follow.objects.create(idol=u3,fans=u5)

# User.objects.filter(idol__fans__name = '王五')  # u3的所有偶像
# User.objects.filter(fans__idol__name = '王五')  # u3的所有偶像
